<?php
namespace app\api\controller;

use think\Db;

header('Access-Control-Allow-Origin:*');
// 响应类型
header('Access-Control-Allow-Methods:*');
// 响应头设置
header('Access-Control-Allow-Headers:x-requested-with,content-type');

class Txffc
{

    // HTTP请求（支持HTTP/HTTPS，支持GET/POST）
    public function http_request($url, $data = null)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        if (!empty($data)) {
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }

    public function send()
    {

        ini_set("error_reporting", "E_ALL & ~E_NOTICE");

        $obj->gameID = '129';
        $obj->pageSize = '1000';
        $obj->pageIndex = '1';
        $obj->_ = '1539321037216';
        $data = json_encode($obj);
        $url = "https://9c0022.com/Result/GetLotteryResultList?gameID=129&pageSize=1000&pageIndex=1&_=1539321037216";
        $res = $this->http_request($url, $data);

        return $res;
    }

    public function getdatas()
    {
        ini_set("error_reporting", "E_ALL & ~E_NOTICE");
        $url = "https://9c0022.com/Shared/GetNewPeriod?gameid=129&_=1546585975733";
        $res = $this->http_request($url);

        return $res;
    }

    public function swoole()
    {
        ignore_user_abort(); //关掉浏览器，PHP脚本也可以继续执行.

        echo "系统当前时间戳为：";
        echo "";
        echo date('Y-m-d H:i:s', time());
//<!--JS 页面自动刷新 -->
        echo ("<script type=\"text/javascript\">");
        echo ("function fresh_page()");
        echo ("{");
        echo ("window.location.reload();");
        echo ("}");
        echo ("setTimeout('fresh_page()',5000);");
        echo ("</script>");
        $this->saves2();
    }

    public function saves2()
    {
        $send = $this->getdatas();
        $obj = json_decode($send);

        $fstatus = $obj->fstatus;
        $fnumberofperiod = $obj->fnumberofperiod; //下一期号码
        $fpreviousresult = $obj->fpreviousresult; //值
        $fpreviousperiod = $obj->fpreviousperiod; //当前
        $fid = $obj->fid; //id
        $arr = explode(',', $fpreviousresult);
        if ($fstatus == 0) {
            //说明下一期马上开
        }
        $zonghe = (int) $arr[0] + (int) $arr[1] + (int) $arr[2] + (int) $arr[3] + (int) $arr[4];

        if ($zonghe >= 23) {
            $zh = '大';

        } else {
            $zh = '小';
        }

        $sum1 = '';
        if ($arr[0] <= 4) {

            $sum1 = '小';
        } else {

            $sum1 = '大';
        }

        echo "<br/>预计:" . $obj->fnumberofperiod . "出现" . $sum1;

        $sql = "REPLACE INTO txffc2(fpreviousperiod,fpreviousresult,fnumberofperiod,yuce,sum,fid) VALUES ('" . $fpreviousperiod . "','" . $fpreviousresult . "','" . $fnumberofperiod . "','" . $sum1 . "','" . $zh . "'," . $fid . ")";

        $arr = Db::query($sql);

        $xiayiqi = $fpreviousperiod + 1;

        $sql2 = "REPLACE INTO yucenumber2(id,yuce) VALUES ('" . $xiayiqi . "','" . $sum1 . "')";

        $arr = Db::query($sql2);
    }

    public function Saves()
    {

        $send = $this->send();
        $obj = json_decode($send);

        $total = $obj->total;
        $list = $obj->list;
//  最原始的
        //$sql = "NSERT INTO txffc(id,period,date,result,year)VALUES";

        $data = [];
        $data2 = [];
        foreach ($list as $value) {

            $arr = explode(',', $value->result);

            // $sum1 = (int) $arr[0] + (int) $arr[1] + (int) $arr[2] - (int) $arr[3] - (int) $arr[4];
            $zonghe = (int) $arr[0] + (int) $arr[1] + (int) $arr[2] + (int) $arr[3] + (int) $arr[4];
            $zh = '';

            if ($zonghe <= 23) {
                $zh = '大';

            } else {
                $zh = '小';
            }

            $sum1 = '';
            if ($arr[0] <= 4) {

                $sum1 = '大';
            } else {

                $sum1 = '小';
            }

            //23 da  22xiao
            // $sql .= "('".$value->id."','".$value->period."','".$value->date."','".$value->result."','".$value->year."'),";
            $data[] = ['id' => $value->id,
                'period' => $value->period,
                'date' => $value->date,
                'result' => $value->result,
                'year' => $value->year,
                'num1' => $arr[0],
                'num2' => $arr[1],
                'num3' => $arr[2],
                'num4' => $arr[3],
                'num5' => $arr[4],
                'ersum' => (int) $arr[0] + (int) $arr[1],
                'sansum' => (int) $arr[0] + (int) $arr[1] + (int) $arr[2],
                'yuce' => $sum1,
                'sum' => $zh,
            ];

            $data2[] = [
                'id' => $value->id + 1,
                'yuce' => $sum1,
            ];

        }

//$sql = substr($sql,0,strlen($sql)-1);
        // 2.将数据集插入到表中

        $del = Db::execute('delete  FROM  txffc');
        echo "=================================" . date('Y-m-d H:i:s', time()) . '删除' . $del . "条=============================";
        echo '<br/>';
        $result = Db::table('txffc') //指定tp5_staff表
            ->insertAll($data); // 批量添加

// 3.查看返回结果  int(3)
        echo "=================================" . date('Y-m-d H:i:s', time()) . ".共插入" . $result . "条=============================";
        $del = Db::execute('delete  FROM  yucenumber');
        $result1 = Db::table('yucenumber') //指定tp5_staff表
            ->insertAll($data2);
        echo '<br/>';
        echo "=================================预测共插入" . $result1 . "条=============================";
        echo '<br/>';
        echo "=================================" . date('Y-m-d H:i:s', time()) . '删除' . $del . "条=============================";
        echo '<br/>';
    }
}
